﻿namespace _2022年的日历
{
    internal class Program
    {
        const int monthWidth = 35;
        const int monthHeight = 9;
        const int dayWidth = 4;
        static void Main(string[] args)
        {
            int x = 0;
            int y = 0;
            //设置初始日期为2022.1.1;循环继续条件为年份不为2023；末尾循环体为每日加1
            for (DateTime theDay = new DateTime(2022, 1, 1); theDay.Year != 2023; theDay = theDay.AddDays(1))
            {
                //记录每天是星期几
                int WeekDay = (int)theDay.DayOfWeek;
                if (theDay.Day == 1)
                {
                    //确定每个月的位置并输出
                    x = ((theDay.Month - 1) % 3) * monthWidth;
                    y = ((theDay.Month - 1) / 3) * monthHeight;
                    Console.SetCursorPosition(x, y);
                    Console.Write($"{theDay.Month}月".PadLeft(4*dayWidth, ' '));
                    //确定星期几的位置
                    y++;
                    Console.SetCursorPosition(x, y);
                    Console.Write("  日  一  二  三  四  五  六  ");
                    //确定每个月1号的位置
                    y++;
                    x = ((theDay.Month - 1) % 3) * monthWidth+WeekDay*dayWidth;
                    Console.SetCursorPosition(x, y);
                    Console.Write("1".PadLeft(dayWidth, ' '));
                }
                //输出其他日期的位置
                //若是周六，则换行继续输出
                if(WeekDay==6)
                {
                    x= ((theDay.Month - 1) % 3) * monthWidth + WeekDay * dayWidth;
                    Console.SetCursorPosition(x, y);
                    Console.Write($"{theDay.Day}".PadLeft(dayWidth, ' '));
                    //使光标移动到下一行开始处
                    y++;
                    x -= WeekDay * dayWidth;                  
                }
                //若不是周六，则依次输出
                else
                {
                    Console.SetCursorPosition(x, y);
                    Console.Write($"{theDay.Day}".PadLeft(dayWidth, ' '));
                    //向右移动依次输出每一天
                    x += dayWidth;                 
                }         
            }
            y++;
            Console.SetCursorPosition(x, y); 
        }
    }
}